#include "..\include\frustum.h"
frustum::frustum(ICamera *pCamera)
	: m_matViewProj(pCamera->GetViewProjectionMatrix())
{
	_init();
}
frustum::frustum(matrix matViewProj)
	: m_matViewProj(matViewProj)
{
	_init();
}
frustum::frustum(void)
	: m_matViewProj()
{
	_init();
}
void frustum::_init()
{
	const float *pMatProj = m_matViewProj.DataPtr();

	//Planes from Projection-View matrix
	m_frusumPlanes[PLANE_LEFT] = plane(
		pMatProj[MAT_INDEX(3,0)] + pMatProj[MAT_INDEX(0,0)], 
		pMatProj[MAT_INDEX(3,1)] + pMatProj[MAT_INDEX(0,1)],
		pMatProj[MAT_INDEX(3,2)] + pMatProj[MAT_INDEX(0,2)], 
		pMatProj[MAT_INDEX(3,3)] + pMatProj[MAT_INDEX(0,3)]);
	m_frusumPlanes[PLANE_RIGHT] = plane(
		pMatProj[MAT_INDEX(3,0)] - pMatProj[MAT_INDEX(0,0)], 
		pMatProj[MAT_INDEX(3,1)] - pMatProj[MAT_INDEX(0,1)],
		pMatProj[MAT_INDEX(3,2)] - pMatProj[MAT_INDEX(0,2)], 
		pMatProj[MAT_INDEX(3,3)] - pMatProj[MAT_INDEX(0,3)]);
	m_frusumPlanes[PLANE_TOP] = plane(
		pMatProj[MAT_INDEX(3,0)] + pMatProj[MAT_INDEX(1,0)], 
		pMatProj[MAT_INDEX(3,1)] + pMatProj[MAT_INDEX(1,1)],
		pMatProj[MAT_INDEX(3,2)] + pMatProj[MAT_INDEX(1,2)], 
		pMatProj[MAT_INDEX(3,3)] + pMatProj[MAT_INDEX(1,3)]);
	m_frusumPlanes[PLANE_BOTTOM] = plane(
		pMatProj[MAT_INDEX(3,0)] - pMatProj[MAT_INDEX(1,0)], 
		pMatProj[MAT_INDEX(3,1)] - pMatProj[MAT_INDEX(1,1)],
		pMatProj[MAT_INDEX(3,2)] - pMatProj[MAT_INDEX(1,2)], 
		pMatProj[MAT_INDEX(3,3)] - pMatProj[MAT_INDEX(1,3)]);
	m_frusumPlanes[PLANE_NEAR] = plane(
		pMatProj[MAT_INDEX(3,0)] + pMatProj[MAT_INDEX(2,0)], 
		pMatProj[MAT_INDEX(3,1)] + pMatProj[MAT_INDEX(2,1)],
		pMatProj[MAT_INDEX(3,2)] + pMatProj[MAT_INDEX(2,2)], 
		pMatProj[MAT_INDEX(3,3)] + pMatProj[MAT_INDEX(2,3)]);
	m_frusumPlanes[PLANE_FAR] = plane(
		pMatProj[MAT_INDEX(3,0)] - pMatProj[MAT_INDEX(2,0)], 
		pMatProj[MAT_INDEX(3,1)] - pMatProj[MAT_INDEX(2,1)],
		pMatProj[MAT_INDEX(3,2)] - pMatProj[MAT_INDEX(2,2)], 
		pMatProj[MAT_INDEX(3,3)] - pMatProj[MAT_INDEX(2,3)]);
}
int frustum::CheckBox(const AABoundingBox &box) const
{
}
int frustum::CheckPoint(const vector3 &point) const
{
	for(int i = 0; i < PLANE_COUNT; i++)
	{
		if(!m_frusumPlanes[i].PointFront(point))
		{
			return false;
		}
	}
	return true;
}
frustum::~frustum(void)
{
}
